package org.guegan.util.dircompare.bean;

import java.io.File;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;

@Entity
@NamedQueries({
	@NamedQuery(name="FileBean.deleted-alone", query="select f from FileBean f, DirBean d where f.present = 0 and f.parent = d and d.present != 0"),
	@NamedQuery(name="FileBean.deleted-all", query="select f from FileBean f where f.present = 0")
})
public class FileBean {
	private Long id;

	@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}

	private String name;
	private Date time;
	private DirBean parent;
	private String signature;
	private Long size;
	private boolean present = true;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Date getTime() {
		return time;
	}
	
	public void setTime(Date time) {
		this.time = time;
	}

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
	public DirBean getParent() {
		return parent;
	}

	public void setParent(DirBean parent) {
		this.parent = parent;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public void setSize(Long size) {
		this.size = size;
	}

	public Long getSize() {
		return size;
	}

	public void setPresent(boolean present) {
		this.present = present;
	}

	public boolean isPresent() {
		return present;
	}
	
	@Transient
	public File getFile() {
		File ret = null;
		File parent = this.parent != null ? this.parent.getFile() : null;
		if (parent != null) {
			ret = new File(parent, name);
		}
		else {
			ret = new File(name);
		}
		return ret;
	}

	public void setFile(File file) {
		setName(file.getName());
		setTime(new Date(file.lastModified()));
		setSize(file.length());
	}

}
